package com.ning.memoryleak;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.ning.memoryleak.activity.HorizontalScrollViewActivity;
import com.ning.memoryleak.activity.LeakThreadActivity;
import com.ning.memoryleak.app.App;
import com.squareup.leakcanary.RefWatcher;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @作者: njb
 * @时间: 2019/11/20 13:12
 * @描述: handler引起的内存泄露
 */
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private TextView textView;
    private Button button;
    private static boolean  isStop = false;
    private static Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            while (!isStop){
                //执行操作
                try {
                    Thread.sleep(50000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    private final static int COUNT = 1;
    Timer timer;


    private void initView() {
        textView = findViewById(R.id.tv_text);
        button = findViewById(R.id.btn_start);
        if (timer == null) {
            timer = new Timer();
            timer.schedule(new TimerTask() {

                @Override
                public void run() {
                    mHandler.sendEmptyMessage(1);
                }
            }, 0, 1000);
        }
    }

    int time = 10;

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            textView.setText(time + "");
            switch (msg.what) {
                case 1:
                    if (time > 0) {
                        time--;
                    } else {
                        Toast.makeText(MainActivity.this, "倒计时完成", Toast.LENGTH_SHORT).show();
                        Log.d(TAG,"倒计时时间为："+ time);
                        if (timer != null) {
                            timer.cancel();
                            timer = null;
                        }
                    }
                    break;

                default:
                    break;
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
        }
        isStop = true;
        RefWatcher refWatcher = App.getRefWatcher(this);
        refWatcher.watch(this);
    }

    public void clickView(View view) {
        switch (view.getId()) {
            case R.id.btn_start:
                Toast.makeText(MainActivity.this, "View是" + view, Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this, LeakThreadActivity.class));
                break;
            case R.id.tv_text:
                Log.e("View", view.toString());
                Toast.makeText(MainActivity.this, "View是" + view, Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this, HorizontalScrollViewActivity.class));
                break;
        }
    }
}
